//
//  MainSelectTypeViewController.m
//  Application
//
//  Created by 薛小刚 on 2017/11/30.
//  Copyright © 2017年 薛小刚. All rights reserved.
//

#import "MainSelectTypeViewController.h"
#import "MainSelectTypeView.h"


@interface MainSelectTypeViewController ()<UIScrollViewDelegate, MainSelectTypeViewDelegate>

@property (nonatomic, assign) CGRect myFrame;
/// 滚动视图
@property (nonatomic, strong) UIScrollView *containerView;

@property (nonatomic, strong) MainSelectTypeView * selectTypeView;

@end

@implementation MainSelectTypeViewController


- (instancetype) initWithFrame:(CGRect) frame titleArray:(NSArray *) array {
    if (self = [super init]) {
        self.view.backgroundColor = [UIColor color_content_background_color];
        _myFrame = frame;
        if (ISNULL(array)) {
            
        } else {
            
            _selectTypeView = [[MainSelectTypeView alloc] initWithFrame:CGRectMake(0, 0, _myFrame.size.width, 55) withDataArray:array];
            _selectTypeView.type = self.type;
            [self.view addSubview:_selectTypeView];
            [_selectTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.view.mas_top);
                make.left.equalTo(self.view.mas_left);
                make.right.equalTo(self.view.mas_right);
                make.height.equalTo(@(55));
            }];
            _selectTypeView.delegate = self;
        }
        [self containerViewSetting];
    }
    return self;
}

- (void)setType:(B_SelectType)type {
    _type = type;
    self.selectTypeView.type = self.type;
}

- (void) setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    _selectTypeView = [[MainSelectTypeView alloc] initWithFrame:CGRectMake(0, 0, _myFrame.size.width, 40) withDataArray:_titleArray];
    [self.view addSubview:_selectTypeView];
    self.selectTypeView.type = self.type;
    [_selectTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(55));
    }];
    _selectTypeView.delegate = self;
}

- (void)containerViewSetting {
    _containerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, _myFrame.size.width, _myFrame.size.height - 40)];
    _containerView.backgroundColor = [UIColor whiteColor];
    _containerView.showsVerticalScrollIndicator = NO;
    _containerView.showsHorizontalScrollIndicator = NO;
    _containerView.delegate = self;
    _containerView.pagingEnabled = YES;
    _containerView.bounces = NO;
    [self.view addSubview:_containerView];
}

- (void) setSelectIndex:(int)selectIndex {
    _selectIndex = selectIndex;

    _containerView.contentOffset = CGPointMake(_selectIndex * _myFrame.size.width, 0);
    _selectTypeView.selectIndex = _selectIndex;
}

- (void) setViewControllers:(NSArray *)viewControllers {
    _viewControllers = viewControllers;
    _containerView.contentSize = CGSizeMake(viewControllers.count * _myFrame.size.width, _myFrame.size.height - 40);
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *viewController = viewControllers[i];
        viewController.view.frame = CGRectOffset(_containerView.bounds, i * _myFrame.size.width, 0);
        [_containerView addSubview:viewController.view];
        [self addChildViewController:viewController];
    }
}

#pragma mark -> scrollView的代理方法
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat contentX = scrollView.contentOffset.x;
    [_selectTypeView setFrameOfLineView:contentX];
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat contentX = scrollView.contentOffset.x;
    [_selectTypeView setSelectBtn:contentX];
    if (self.block) {
        self.block(contentX / _myFrame.size.width);
    }
}
#pragma mark -> 选择按钮的代理方法
- (void) mainSelectTypeWithBtn:(UIButton *)btn {
    [UIView animateWithDuration:0.3 animations:^{
        _containerView.contentOffset = CGPointMake((btn.tag - 1) * _myFrame.size.width, 0);
    }];
    if (self.block) {
        self.block(btn.tag - 1);
    }
}


@end
